<?php
class Smithy_Data_Table_Col
{
    /**
     * The options for the col.
     *
     * @var array
     */
    protected $_options = array(
        'headerTitle'       => 'Column',
        'columnName'        => null,
        'methodName'        => null,
        'methodParamArray'  => array(),
        'sortable'          => false,
        'width'             => null,
        'type'              => 'data'
    );

    /**
     * The td tag's attributes.
     *
     * @var array
     */
    protected $_attributes = array();

    /**
     * The th tag's attributes.
     *
     * @var array
     */
    protected $_headerAttributes = array();


    /**
     * Constructor
     *
     * @return void
     */
    function __construct ($options, $headerAttribs = array(), $attribs = array())
    {
        /*if (array_key_exists('headerTitle', $options)) {
            $this->setHeaderTitle($options['headerTitle']);
        }

        if (array_key_exists('columnName', $options)) {
            $this->setColumnName($options['columnName']);
        }

        if (array_key_exists('methodName', $options)) {
            $this->setMethodName($options['methodName']);
        }

        if (array_key_exists('sortable', $options)) {
            $this->setSortable($options['sortable']);
        }

        if (array_key_exists('width', $options)) {
            $this->setWidth($options['width']);
        }*/

        $this->_options = array_merge($this->_options, $options);
        $this->_headerAttributes = array_merge($this->_headerAttributes, $headerAttribs);
        $this->_attributes = array_merge($this->_attributes, $attribs);
    }

    /**
     * Renders the col's header column.
     *
     * @return string
     */
    public function renderHeader()
    {
        return '<th '.$this->_renderAttributes($this->_headerAttributes).'><strong>'.$this->_options['headerTitle'].'</strong></th>' . "\n";
    }

    /**
     * Renders the col's column.
     *
     * @param object $data
     * @return string
     */
    public function render($data)
    {
        if (null !== $methodName = $this->getMethodName()) {
            $value = call_user_func_array($methodName, $this->_options['methodParamArray']);
        } else {
            $columnName = $this->getColumnName();
            $value = $data->$columnName;
        }

        switch ($this->_options['type']) {
            case 'link':    $value = $this->_renderLink($value);
                            break;
        }

        return '<td '.$this->_renderAttributes($this->_attributes).'>'.$value.'</td>' . "\n";
    }

    /**
     * Renders the table's attributes.
     *
     * @param array $attribs
     * @return unknown
     */
    protected function _renderAttributes($attribs)
    {
        $src = '';
        foreach ($attribs as $name => $value) {
            $src .= $name . '="' . $value . '" ';
        }
        return $src;
    }

    /**
     * Adds a new or sets an existing attribute for the col.
     *
     * @param string $name
     * @param string $value
     * @return Smithy_Data_Table_Col
     */
    public function addAttribute($name, $value)
    {
        $this->_attributes[$name] = $value;
        return $this;
    }

    /**
     * Renders a link.
     *
     * @param string $data
     * @return string
     */
    protected function _renderLink($data)
    {
        return '<a href="' . $this->_options['url'] . $data . '" >' . $this->_options['linkText'] . '</a>';
    }

    /**
     * Sets the attributes for the col.
     *
     * @param array $attribs
     * @return Smithy_Data_Table_Col
     */
    public function setAttributes($attribs)
    {
        foreach ($attribs as $name => $value) {
            $this->addAttribute($name, $value);
        }
        return $this;
    }

    /**
     * Sets the col's header.
     *
     * @param string $headerTitle
     * @return Smithy_Data_Table_Col
     */
    public function setHeaderTitle($headerTitle)
    {
        $this->_options['headerTitle'] = $headerTitle;
        return $this;
    }

    /**
     * Sets the col's column name
     *
     * @param string|null $columnName
     * @return Smithy_Data_Table_Col
     */
    public function setColumnName($columnName)
    {
        $this->_options['columnName'] = $columnName;
        return $this;
    }

    /**
     * Sets the col's method to be called.
     *
     * @param string|null $methodName
     * @return Smithy_Data_Table_Col
     */
    public function setMethodName($methodName)
    {
        $this->_options['methodName'] = $methodName;
        return $this;
    }

    /**
     * Sets the col to be sortable
     *
     * @param bool $sortable
     * @return Smithy_Data_Table_Col
     */
    public function setSortable($sortable)
    {
        $this->_options['sortable'] = $sortable;
        return $this;
    }

    /**
     * Sets the col's width
     *
     * @param int $width
     * @return Smithy_Data_Table_Col
     */
    public function setWidth($width)
    {
        $this->_options['width'] = $width;
        return $this;
    }

    /**
     * Gets the col's header.
     *
     * @return string
     */
    public function getHeaderTitle()
    {
        return $this->_options['headerTitle'];
    }

    /**
     * Gets the col's column name
     *
     * @return string|null
     */
    public function getColumnName()
    {
        return $this->_options['columnName'];
    }

    /**
     * Gets the col's method to be called.
     *
     * @return string|null
     */
    public function getMethodName()
    {
        return $this->_options['methodName'];
    }

    /**
     * Gets the col to be sortable
     *
     * @return bool
     */
    public function getSortable()
    {
        return $this->_options['sortable'];
    }

    /**
     * Gets the col's width
     *
     * @return int
     */
    public function getWidth()
    {
        return $this->_options['width'];
    }
}
